#!/usr/bin/python
# -*- coding: UTF-8 -*-
# @author:qiaofinn
# @file:views.py
# @time:2020/09/10
from django.db import transaction
from rest_framework import generics, views
from utils import response_error, response_ok
from apps.adorn_service.models import OrderService, AdornService
from .serializers import OrderAppServiceSerializer, AdornServiceAppSeializer


class AdornServiceListView(generics.ListAPIView):
    queryset = AdornService.objects.filter()
    serializer_class = AdornServiceAppSeializer

    # def get(self, request, *args, **kwargs):
    #     serializer = self.serializer_class(self.get_queryset(), many=True)
    #     return response_ok(serializer.data)

    # def paginate_queryset(self, queryset):
    #     return None


class OrderServiceViewSet(views.APIView):

    def post(self, request, *args, **kwargs):
        try:
            code = request.data['code']
            adorn_service = AdornService.objects.filter(code=code).first()
            if not adorn_service:
                adorn_service = AdornService.objects.filter(code='other').first()

            with transaction.atomic():
                OrderService.objects.create(adornService=adorn_service, name=request.data['name'],
                                            mobile=request.data['mobile'])

        except Exception as e:
            return response_error(str(e))
        return response_ok()
